import request from "../../utils/request";
Page({
  data: {
    activesList: [
      {
        id: 1,
        cover: '/images/actives1.png',
        title: '法律宣讲 熟知诈骗手段',
        description: '文化教育类活动，包含法律知识讲解等', 
        userIcon: '/images/avatar.png',
        userName: '布布镜子',
        scale: 1,
        opacity: 1,
        category: '文化教育',
        browseCount: 0 
      },
      {
        id: 2,
        cover: '/images/actives2.png',
        title: '感知春色 小区绕城健身慢跑',
        description: '体育健康类活动，围绕小区慢跑锻炼',
        userIcon: '/images/avatar.png',
        userName: '彼酷哩',
        scale: 1,
        opacity: 1,
        category: '体育健康',
        browseCount: 0
      },
      {
        id: 3,
        cover: '/images/actives3.png',
        title: '小区八旬独身老人慰问',
        description: '志愿服务类活动，关爱老人行动',
        userIcon: '/images/avatar.png',
        userName: '哈哈哈',
        scale: 1,
        opacity: 1,
        category: '志愿服务',
        browseCount: 0
      },
      {
        id: 4,
        cover: '/images/actives4.jpg',
        title: '团圆佳节 中秋灯谜会',
        description: '节日主题类活动，中秋灯谜庆祝',
        userIcon: '/images/avatar.png',
        userName: '酷哩',
        scale: 1,
        opacity: 1,
        category: '节日主题',
        browseCount: 0
      }
    ],
    categories: [
      { id: 1, icon: '/images/wenhua-icon.png', title: '文化教育' },
      { id: 2, icon: '/images/tiyu-icon.png', title: '体育健康' },
      { id: 3, icon: '/images/zhiyuan-icon.png', title: '志愿服务' },
      { id: 4, icon: '/images/jieri-icon.png', title: '节日主题' }
    ],
    searchRecords: [],
    searchKeyword: ''
  },

  onInputChange(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  onSearch() {
    const keyword = this.data.searchKeyword.trim();
    if (keyword) {
      if (!this.data.searchRecords.includes(keyword)) {
        this.setData({
          searchRecords: [keyword, ...this.data.searchRecords]
        });
      }
      console.log('搜索关键词:', keyword);
    }
  },

  navigateToDetail(e) {
    const id = e.currentTarget.dataset.id;
    const targetActivity = this.data.activesList.find(item => item.id === id);
    
    if (targetActivity) {
      // 浏览数+1
      targetActivity.browseCount++;
      this.setData({
        activesList: this.data.activesList
      });

      const title = targetActivity.title;
      if (!this.data.searchRecords.includes(title)) {
        this.setData({
          searchRecords: [title, ...this.data.searchRecords]
        });
      }
    }

    this.setData({
      activesList: this.data.activesList.map(item => ({
        ...item,
        scale: 1,
        opacity: 1
      }))
    });

    this.setData({
      activesList: this.data.activesList.map(item => 
        item.id === id ? { ...item, scale: 0.98, opacity: 0.9 } : item
      )
    });

    wx.navigateTo({
      url: `/pages/activesdetail/activesdetail?id=${id}`
    });
  },

  async onShow() {
    let data=await request.get("actives/")
    data=data.map(item => ({
      ...item,
      scale: 1,
      opacity: 1
    }));
    this.setData({
      activesList:data
    });
  }
})